<template>
  <!-- 代理返佣 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row></template>
        <template v-slot:actions>
          <el-button type="primary" v-permission="" size="mini" icon="el-icon-plus" 
          @click="(isShowDetails = true),(checkedItem = null)">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{ "" }}</el-radio>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_name" label="代理名称" width="120" />
          <el-table-column align="center" prop="a_level" label="代理等级" width="100" />
          <el-table-column align="center" prop="a_item" label="返佣货币" width="120">
            <template slot-scope="scope">
              {{ propLabel(scope.row.a_item) }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_firstgent" label="A级代理返佣">
            <template slot-scope="scope">
              {{ scope.row.a_firstgent/100 }}%
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_secondgent" label="B级代理返佣">
            <template slot-scope="scope">
              {{ scope.row.a_secondgent/100 }}%
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_thirdgent" label="C级代理返佣">
            <template slot-scope="scope">
              {{ scope.row.a_thirdgent/100 }}%
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_fourthgent" label="D级代理返佣">
            <template slot-scope="scope">
              {{ scope.row.a_fourthgent/100 }}%
            </template>
          </el-table-column>
          <el-table-column align="left" prop="operation" label="操作" width="200">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)"><i class="el-icon-edit-outline" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click.stop="deleteItem(scope.row)"><i class="el-icon-delete" /></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" @onHideDetails="onHideDetails" :data="checkedItem" :propDataArray="propDataArray" />
    </div>
  </div>
</template>

<script>
import { throttle } from "@/utils/index";
import { 
  getRebate,
  delRebate
 } from "@/api/game";
import PageDetails from "./page-details.vue";
import SearchTable from "@/components/SearchTable";
export default {
  name: "rebate",
  components: {
    PageDetails,
    SearchTable,
  },
  data () {
    return {
      dataList: [],
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
      },
      propDataArray: this.$store.state.commonState.propDataArray,
    };
  },
  created () {
    // this.getDataList();
  },
  activated () {
    this.propDataArray=this.$store.state.commonState.propDataArray
    this.getDataList();
  },
  methods: {
    // 获取快捷聊天表
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await getRebate(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach((item) => {
            item.isChecked = false;
          });
          that.currentTotal = response.data.total;
          that.dataList = response.data.list;
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false;
    },
    // 时间类型
    propLabel(cellValue){
      return (this.propDataArray.find((item) => item.value === cellValue)?.label || "-");
    },
    // 编辑
    editItem (row) {
      let that = this;
      that.isShowDetails = true;
      that.checkedItem = row;
      that.handleCurrentChange(row);
    },
    // 删除
    deleteItem (row) {
      let that = this;
      that.handleCurrentChange(row);
      that.$confirm(`是否删除【${row.a_chat_remark}】聊天配置`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
          delRebate({
            id: row.id,
          }).then(function (res) {
            that.getDataList();
            that.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => { });
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val;
      this.dataList.map((item) => {
        if (item.id === val.id) {
          item.isChecked = true;
        } else {
          item.isChecked = false;
        }
      });
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false;
      if (show) {
        this.getDataList();
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
      };
      this.getDataList();
    }, 2000),
  },
};
</script>

<style lang="scss" scoped></style>
